package com.guoqing.example.date;

import org.springframework.stereotype.Service;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @ClassName MyDate
 * @Description TODO
 * @Author ZhangGuoQing
 * @Date 2020/8/30/0030 22:04
 */
@Service
public class MyDateUtil {

    private static final String ofPatternLong = "yyyy-MM-dd HH:mm:ss";
    private static final String ofPatternShort = "yyyy-MM-dd";

    /**
     * 根据 LocalDateTime 获取 Date
     * @param localDateTime LocalDateTime
     * @return Date
     */
    public static Date getDateOfLocalDateTime(LocalDateTime localDateTime){
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 时间字符串转换 localDateTime
     * @param dateTimeString String
     */
    public static LocalDateTime getLocalDateTimeOfString(String dateTimeString) {
        return LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(ofPatternLong));
    }

    /**
     * date 转 localDateTime
     * @param date Date
     */
    public static LocalDateTime getLocalDateTimeOfDate(Date date) {
        Instant instant = date.toInstant();
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * localDateTime 转 String
     * @param localDateTime LocalDateTime
     * @return String
     */
    public static String getStringOfLocalDateTime(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(ofPatternLong));
    }

    /**
     * lang 类型的 timestamp 转为 LocalDateTime
     * @param timestamp long
     * @return localDateTime
     */
    public static LocalDateTime getLocalDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * LocalDateTime转为long类型的timestamp
     * @param localDateTime time
     * @return long
     */
    public static long getTimestampOfLocalDateTime(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 计算两时间间隔
     * @param startTime  LocalDateTime
     * @param endTime  LocalDateTime
     * @param unit  间隔类型
     * @return long
     */
    public static long betweenLocalDateTimeOfUnit(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit unit){
        long between = unit.between(startTime, endTime);
        return Math.abs(between);
    }

    /**
     * 计算两时间间隔
     * @param startTime  Date
     * @param endTime  Date
     * @param unit  间隔类型
     * @return long
     */
    public static long betweenLocalDateTimeOfUnit(Date startTime, Date endTime, ChronoUnit unit){
        LocalDateTime start = getLocalDateTimeOfDate(startTime);
        LocalDateTime end = getLocalDateTimeOfDate(endTime);
        long between = unit.between(start, end);
        return Math.abs(between);
    }

    /**
     * 根据 yyyy-MM-dd 类型的字符串获取 LocalDate
     * @param dateString yyyy-MM-dd 类型字符串
     * @return LocalDate
     */
    public static LocalDate getLocalDateOfString(String dateString) {
        return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(ofPatternShort));
    }

    /**
     * 根据 yyyy-MM-dd 类型的字符串获取 LocalDate
     * @param localDate  localDate
     * @return String
     */
    public static String getStringOfLocalDate(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern(ofPatternShort));
    }

    /**
     * 根据 yyyy-MM-dd 类型的字符串获取 LocalDate
     * @return Date
     */
    public static Date getDateOfLocalDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 计算给定日期向前推迟几个月
     * @param localDate LocalDate
     * @param count 向前推迟的的数字
     */
    public static void minus(LocalDate localDate, int count){
        LocalDate localDate1 = localDate.minusMonths(count);
    }

    /**
     * 计算给定日期向前推迟几个月
     * @param localDateTime LocalDateTime
     * @param count 向前推迟的的数字
     */
    public static LocalDateTime minus(LocalDateTime localDateTime, int count){
        return localDateTime.minusMonths(count);
    }

    /**
     * 测试
     *
     * @param args args
     */
    public static void main1(String[] args) {
        String start = "2020-08-31 10:10:10";
        String end = "2020-08-31 09:10:10";
        long l = betweenLocalDateTimeOfUnit(getLocalDateTimeOfString(start), getLocalDateTimeOfString(end), ChronoUnit.MINUTES);
        System.out.println(l);
        System.out.println(Math.abs(l));

        start = "2020-08-31";
        System.out.println(getLocalDateOfString(start));
    }
    
    /**
     * 方法1：计算两日期相差 几小时几分钟几秒
     * @param start 开始时间
     * @param end 结束时间
     * @author leining 
     * @date 2020/10/14 13:36 
     * @return void
     */
    public static void betweenDate(LocalDateTime start, LocalDateTime end){
        long sec = ChronoUnit.SECONDS.between(start, end);
        long l1 = sec / 3600;
        long l = sec % 3600;
        long l2 =  l / 60;
        long l3 = l % 60;
        System.out.println("间隔："+l1+"小时"+ l2+"分钟"+l3+"秒");
    }
    

    /**
     * 方法2：计算两日期相差 几小时几分钟几秒
     * @param start 开始时间
     * @param end 结束时间
     * @author leining 
     * @date 2020/10/14 13:36 
     * @return void
     */
    public static void betweenDate2(LocalDateTime start, LocalDateTime end) {
        Duration between = Duration.between(start, end);
        long l1 = between.toHours();
        long l2 = between.toMinutes() % 60;
        long seconds = Duration.between(start, end).getSeconds();
        long l3 = seconds % 3600 / 60;
        System.out.println("间隔：" + l1 + "小时" + l2 + "分钟" + l3 + "秒");
    }

    public static void main2(String[] args) {
        String orderDate = "20201022";
        LocalDate parse = LocalDate.parse(orderDate, DateTimeFormatter.ofPattern("yyyyMMdd"));
        String format = parse.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static void main(String[] args) {
        System.out.println(LocalDate.now());

    }


}
